package quq.missq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.ImageViewPagerActivity;
import quq.missq.activity.LoginActivity;
import quq.missq.activity.PublishComentActivity;
import quq.missq.activity.TaPageActivity;
import quq.missq.beans.CommentsBean;
import quq.missq.beans.TopicBean;
import quq.missq.beans.UserBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.StringUtilsSec;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/3/15.
 */
public class BigShootDetailAdapter extends BaseAdapter{

    private Context mContext;
    private LinkedList<CommentsBean.Result> results;
    private int targetType = 0;   //0大招 1话题
    private int id = 0; // 大招或者话题id

    public BigShootDetailAdapter(Context context,int targetType,int id){
        this.mContext = context;
        this.targetType = targetType;
        this.id = id;
    }

    public void setData(LinkedList<CommentsBean.Result> results){
        this.results = results;
    }

    public LinkedList<CommentsBean.Result> getData(){
        return results;
    }

    @Override
    public int getCount() {
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        return results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if(null == convertView){
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.comment_adapter,null);
            holder.avtar = (NetworkImageView)convertView.findViewById(R.id.avtar);
            holder.userName = (TextView)convertView.findViewById(R.id.userName);
            holder.toUserName = (TextView)convertView.findViewById(R.id.toUserName);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.replyLayout = (LinearLayout)convertView.findViewById(R.id.replyLayout);
            holder.attach = (ImageView)convertView.findViewById(R.id.attach);
            holder.content = (TextView)convertView.findViewById(R.id.content);
            holder.gender = (ImageView)convertView.findViewById(R.id.gender);
            holder.zan = (ImageView)convertView.findViewById(R.id.zan);
            holder.zanCount = (TextView)convertView.findViewById(R.id.zanCount);
            holder.commentIcon = (ImageView)convertView.findViewById(R.id.commentIcon);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        final CommentsBean.Result result = results.get(position);
        holder.avtar.setImageUrl(Constants.IMAGE_HOST + result.getAuthor().getAvatar(), VolleyTool.getImageLoader());
        holder.avtar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, TaPageActivity.class);
                intent.putExtra("uid", result.getAuthor().getId());
                intent.putExtra("username", result.getAuthor().getNkname());
                mContext.startActivity(intent);
            }
        });
        holder.userName.setText(result.getAuthor().getNkname());
        holder.content.setText(result.getContent());
        //友好的显示时间
        String friendly_time = StringUtilsSec.friendly_time(result.getCreated());
        holder.time.setText(friendly_time);
        holder.zanCount.setText(result.getPraises()+"");
        int genderType = R.mipmap.boy_icon;
        if(result.getAuthor().getGender() == 1){
            genderType = R.mipmap.boy_icon;
        }else{
            genderType = R.mipmap.girl_icon;
        }
        holder.gender.setImageResource(genderType);
        if(result.getReplyUser() == null){
            holder.replyLayout.setVisibility(View.GONE);
        }else{
            holder.replyLayout.setVisibility(View.VISIBLE);
            holder.toUserName.setText(result.getReplyUser().getNkname());
        }
        holder.commentIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, PublishComentActivity.class);
                intent.putExtra("targetType",targetType);
                intent.putExtra("targetId",id);
                intent.putExtra("parentId",result.getId());
                intent.putExtra("userId",result.getAuthor().getId());
                mContext.startActivity(intent);
            }
        });
        holder.zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UserBean.User user = UserTools.getUser(mContext);
                if(user == null){
                    Intent intent = new Intent();
                    intent.setClass(mContext, LoginActivity.class);
                    mContext.startActivity(intent);
                }else{
                    Map<String,String> params = new HashMap<>();
                    params.put("commentId",result.getId()+"");
                    params.put("acc_token",user.getAcc_token());
                    VolleyTool.get((Activity)mContext,Constants.ZAN_URL,params,new VolleyTool.HTTPListener() {
                        @Override
                        public <T> void onResponse(T t, int requestCode) {
                            try {
                                JSONObject obj = new JSONObject(t.toString());
                                if(obj.getInt("code") >= 0){
                                    int count = Integer.parseInt(holder.zanCount.getText().toString());
                                    holder.zanCount.setText((count+1)+"");
                                    int praises = result.getPraises();
                                    result.setPraises(praises+1);
                                }else{
                                    Toast.makeText(mContext,obj.getString("message"),Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onErrorResponse(VolleyError error, int requestCode) {

                        }
                    },0,null);
                }
            }
        });
        if(result.getAttachs().size() > 0){
            holder.attach.setVisibility(View.VISIBLE);
            ImageLoadUtil.loadImage(holder.attach, Constants.IMAGE_HOST + result.getAttachs().get(0).getThumbnail(), R.mipmap.empty_photo);
            holder.attach.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ImageViewPagerActivity.class);
                    intent.putExtra("position",position);
                    ArrayList<String> urls = new ArrayList<>();
                    //for (int i = 0;i < results.size();i++){
                        //urls.add(Constants.IMAGE_HOST+result.getAttachs().get(0).getPreview());
                    urls.add(Constants.IMAGE_HOST+result.getAttachs().get(0).getOrigin());
                   // }
                    intent.putExtra("netImageUrls",urls);
                    mContext.startActivity(intent);
                }
            });
        }else{
            holder.attach.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class Holder{

        NetworkImageView avtar;
        TextView userName;
        TextView toUserName;
        TextView time;
        LinearLayout replyLayout;
        ImageView attach;
        TextView content;
        ImageView gender;
        ImageView zan;
        TextView zanCount;
        ImageView commentIcon;

    }

}
